package com.filepublic.demo.server.service.impl;

import com.filepublic.demo.common.constant.ExceptionConstant;
import com.filepublic.demo.common.context.ThreadLocalContext;
import com.filepublic.demo.common.exception.BaseLogicException;
import com.filepublic.demo.common.property.TextProperty;
import com.filepublic.demo.common.util.FileUtil;
import com.filepublic.demo.pojo.dto.EmailDto;
import com.filepublic.demo.pojo.entity.EmailEntity;
import com.filepublic.demo.pojo.entity.FileEntity;
import com.filepublic.demo.pojo.entity.UserEntity;
import com.filepublic.demo.pojo.vo.EmailVo;
import com.filepublic.demo.server.mapper.EmailBaseMapper;
import com.filepublic.demo.server.mapper.FileBaseMapper;
import com.filepublic.demo.server.mapper.UserBaseMapper;
import com.filepublic.demo.server.service.AsyncService;
import com.filepublic.demo.server.service.EmailService;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.File;
import java.io.IOException;
import java.time.LocalDate;
import java.util.List;

@Service
public class EmailServiceImpl implements EmailService {

    @Resource
    TextProperty textProperty;

    @Resource
    AsyncService asyncService;

    @Resource
    EmailBaseMapper emailBaseMapper;

    @Resource
    UserBaseMapper userBaseMapper;

    @Resource
    FileBaseMapper fileBaseMapper;

    /**
     * 发邮件
     * @param emailDto
     */
    @Override
    public void sendEmail(EmailDto emailDto) throws IOException {
        //先查询有无该用户
        UserEntity userEntity = userBaseMapper.selectUserById(emailDto.getEmailOwnerId());

        if(userEntity == null){
            throw new BaseLogicException(ExceptionConstant.NO_USER);
        }

        //生成新的文件名
        MultipartFile file = emailDto.getFile();
        String newFileName = FileUtil.buildNewFileName(file);

        //生成保存路径
        String saveFilePath = textProperty.getTextOriginPath()+newFileName;

        //异步保存文件
        asyncService.saveFile(file,new File(saveFilePath));

        //获取userId
        String userId = ThreadLocalContext.getCurrentId();

        //将数据添加到数据库里
        EmailEntity emailEntity = EmailEntity.builder()
                .emailId(null)
                .emailOwnerId(emailDto.getEmailOwnerId())
                .emailSenderId(userId)
                .fileId(emailDto.getFileId())
                .emailTitle(emailDto.getTitle())
                .emailText(emailDto.getText())
                .emailTextPath(newFileName)
                .sendDate(LocalDate.now())
                .emailRead(0)
                .emailStar(0)
                .build();
        emailBaseMapper.insertEmail(emailEntity);
    }

    /**
     * 获取所有邮件
     * @return
     */
    @Override
    public List<EmailVo> listEmail() {
        String userId = ThreadLocalContext.getCurrentId();

        //通过用户id查找对应的邮件
        List<EmailVo> emailVoList = emailBaseMapper.selectEmailById(userId);

        return emailVoList;
    }

    /**
     * 获取文件信息
     * @param fileId
     * @return
     */
    @Override
    public FileEntity getFileInfo(String fileId) {
        FileEntity fileEntity = fileBaseMapper.selectFileById(fileId);
        return fileEntity;
    }

    /**
     * 设置已读
     * @param emailId
     */
    @Override
    public void readEmail(Integer emailId) {
        EmailEntity emailEntity = EmailEntity.builder()
                .emailId(emailId)
                .emailRead(1)
                .build();
        emailBaseMapper.updateBase(emailEntity);
    }

    /**
     * 收藏
     * @param emailId
     */
    @Override
    public void starEmail(Integer emailId, Integer state) {
        EmailEntity emailEntity = EmailEntity.builder()
                .emailId(emailId)
                .emailStar(state)
                .build();
        emailBaseMapper.updateBase(emailEntity);
    }
}


















